package com.card.user;

import java.util.concurrent.locks.ReentrantLock;

import com.card.common.CardContants;
import com.card.game.room.AbstractRoom;
import com.card.game.room.RoomManager;
import com.card.log.LogType;
import com.card.log.LogUtils;
import com.card.match.Match;
import com.card.match.MatchManager;
import com.card.message.JsonMessageCenter;
import com.xiaoao.net.common.GameConstants;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.global.Global;
import com.xiaoao.net.json.message.JsonMessage;
import com.xiaoao.net.utils.AddressUtils;


/**
 * 用户类  
 * @Author: siyunlong
 * @Version: V1.00
 * @Create Date: 2015-5-8下午3:31:40
 */
public class User extends UserBase{
	private transient String ip;
	private transient double gps;//gps
	private transient double gps2;//gps
	private transient MessageQueue messageQueue;
	private transient ReentrantLock lock;
	
	public User(Entity userEntity) {
		super(userEntity);
		this.lock = new ReentrantLock();
	}
	
	public ReentrantLock getLock() {
		return lock;
	}

	@Override
	public String getIp() {
		if(ip != null && !"未知".equals(ip)) {
			return ip;
		}
		try {
			if(ctx != null) {
				String ipPort = ctx.getChannel().getRemoteAddress().toString();
				String ip = ipPort.split(":")[0];
				if(ip.startsWith("/")) {
					ip = ip.substring(1, ip.length());
					return ip;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "未知";
	}
	
	public double getGps() {
		return gps;
	}

	public void setGps(double gps) {
		this.gps = gps;
	}

	public double getGps2() {
		return gps2;
	}

	public void setGps2(double gps2) {
		this.gps2 = gps2;
	}

	/**
	 * 登录检查
	 */
	public void loginChk() {
		//重置最后一次登录
		if(chkResetData()) {
			userStorage().setDayReward(true);
			userStatistics().clear();
		}
	}

	public AbstractRoom getRoom() {
		return RoomManager.getInstance().getRoomById(roomId);
	}
	public AbstractRoom getRoom(int roomId) {
		return RoomManager.getInstance().getRoomById(roomId);
	}
	
	public void startMessageQueue() {
//		this.messageQueue = new MessageQueue(this); 
	}
	//设置房间
	public void setRoom(AbstractRoom room) {
		int roomId = room != null?room.getId():0;
		setRoomId(roomId);
	}
	//设置比赛
	public void setMatch(Match match) {
		String matchId = match != null?match.getId():"";
		setMatchId(matchId);
	}
	
	public Match getMatch() {
		return MatchManager.getInstance().getMatchById(getMatchId());
	}
	public void loginOut() {
		if(this.messageQueue != null) {
			this.messageQueue.shutdown();
		}
	}
	
	public void addMessage2Queue(JsonMessage message) {
		if(this.messageQueue != null) {
			this.messageQueue.addMessage(message);
		}else{
			JsonMessageCenter.getInstance().sendMessage(message, ctx);
		}
	}
	
	public boolean isOnline() {
		try {
			return ctx != null && ctx.getChannel().isConnected();
		} catch (Exception e) {
		}
		return false;
	}
	private transient long lastPingTime = System.currentTimeMillis();
	public void setPing() {
		this.lastPingTime = System.currentTimeMillis();
	}
	public void checkPingTimeOut() {
		if(getRoom() == null && System.currentTimeMillis() - this.lastPingTime > 5*GameConstants.MINUTE) {
			Global.logErrorMessage(getPrimaryKey()+":ping超时");
			UserLogic.getInstance().userLoginOut(this);
		}
	}
	//创建房间时的消费，在代开房间时，消费仍然可以调这个接口
	public void createRoomSpendLog(int GameType,int spend) {
		if(GameType == CardContants.GAMETYPE_SH) {
			LogUtils.addSpendLog(this, LogType.CMJ, spend);
		}else if(GameType == CardContants.GAMETYPE_TGG) {
			LogUtils.addSpendLog(this, LogType.CZJH, spend);
		}else if(GameType == CardContants.GAMETYPE_Ddz) {
			LogUtils.addSpendLog(this, LogType.CDDZ, spend);
		}else if(GameType == CardContants.GAMETYPE_Pdk) {
			LogUtils.addSpendLog(this, LogType.CPDK, spend);
		}else if(GameType == CardContants.GAMETYPE_Nn) {
			LogUtils.addSpendLog(this, LogType.CNN, spend);
		}
	}
	
	public static void main(String[] args) {
		String s = "/192.168.1.72:4756";
		String ip = s.split(":")[0];
		if(ip.startsWith("/")) {
			ip = ip.substring(1, ip.length());
		}
		System.out.println(ip);
	}

	
}
